﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DuplicateFileFinder.Infrastructure;
using DuplicateFileFinder.Controller;

namespace DuplicateFileFinder
{
    public partial class MainForm : Form, IMainForm
    {
        #region Privates

        private FormController formController;
        private String target = null;

        #endregion

        public MainForm()
        {
            InitializeComponent();
            formController = new FormController();
            UIManager.Initialize(this);
        }

        #region IMainForm Members

        public void UpdateStatus(string msg)
        {
            label1.Text = msg;
        }

        public void AddFile(string key, string file)
        {
            if (!treeView1.Nodes.ContainsKey(key))
                treeView1.Nodes.Add(key, file);
            else
                treeView1.Nodes[key].Nodes.Add(file);
        }

        public void UpdateFilesProcessedCount(long count)
        {
            fileProcessedCountlbl.Text = count.ToString();
        }

        public void UpdateDuplicatesFoundCount(long count)
        {
            dupesFoundcountlbl.Text = count.ToString();
        }

        public void ClearTreeView(object obj)
        {
            treeView1.Nodes.Clear();
        }

        #endregion

        #region Event Handlers

        private void button1_Click(object sender, EventArgs e)
        {
            formController.StopScan();
        }

        private void startBtn_Click(object sender, EventArgs e)
        {
            if (target == null)
                setPathToolStripMenuItem_Click_1(null, null);

            if (target != null)
                formController.StartScan(target);
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            formController.StopScan();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            List<String> listOfDuplicateFiles = new List<String>();

            foreach (var node in treeView1.Nodes.OfType<TreeNode>())
                foreach (var dupe in node.Nodes.OfType<TreeNode>())
                    listOfDuplicateFiles.Add(dupe.Text);

            var deleted = formController.Delete(listOfDuplicateFiles);

            if (deleted)
                treeView1.Nodes.Clear();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Developed by Sujith.", "Duplicate File Finder");
        }

        private void setPathToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            target = formController.GetPath();
            
            if (target != null)
                UpdateStatus("Target path : " + target);
        }

        #endregion

        private void MainForm_Load(object sender, EventArgs e)
        {

        }
    }
}
